
package com.example.demo.modules.sys.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.result.PageResult;
import com.example.demo.modules.sys.model.SysDept;
import com.example.demo.modules.sys.model.SysDeptUser;
import com.example.demo.modules.sys.model.SysUser;
import com.example.demo.modules.sys.service.DeptService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

import static com.example.demo.core.entity.ProcessResult.ERROR;

/**
 * @author qjp
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("/sys/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public ModelAndView list() {
        // List<SysUser> userInfoList = userService.getAll(sysUser);
        return new ModelAndView("/modules/sys/sysDept/list");
    }


    @RequestMapping
    public List<SysDept> getAll() {
        return deptService.getAll();
    }

    @RequestMapping("/treeList")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Map<String,Object> treeList() {
        return deptService.treeList();
    }
    @RequestMapping("/getDeptById/{id}")
    public SysDept getDeptById(@PathVariable Integer id){
        return deptService.getDeptById(id);
    }

    @RequestMapping(value = "/saveOrUpdate")
    public ProcessResult saveOrUpdate(SysDept sysDept) {
        try {
            deptService.saveOrUpdate(sysDept);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping(value = "/delete/{id}")
    public ProcessResult delete(@PathVariable Integer id) {
        try {
            deptService.deleteById(id);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }
    //已分配列表
    @RequestMapping("/isAllotUserList")
    public PageResult<SysUser> isAllotUserList(SysUser sysUser,Integer deptId){
        List<SysUser> list= deptService.isAllotUserList(sysUser,deptId);
        if(list.size()>0){
            for (SysUser user:list){
                SysDeptUser deptUser = deptService.getSysDeptUser(user.getId(), deptId);
                if(deptUser!=null){
                    user.setIsmaster(deptUser.getIsmaster());
                }
            }
        }
        return new PageResult<SysUser>(new PageInfo<>(list));
    }

    //未分配列表
    @RequestMapping("/isUnAllotUserList")
    public PageResult<SysUser> isUnAllotUserList(SysUser sysUser,Integer deptId){
        List<SysUser> list= deptService.isUnAllotUserList(sysUser,deptId);
        if(list.size()>0){
            for (SysUser user:list){
                SysDeptUser deptUser = deptService.getSysDeptUser(user.getId(), deptId);
                if(deptUser!=null){
                    user.setIsmaster(deptUser.getIsmaster());
                }
            }
        }
        return new PageResult<SysUser>(new PageInfo<>(list));
    }

    //分配或移除 type 0分配 ,1移除
    @RequestMapping(value = "/allotUserByDept")
    public ProcessResult allotUserByDept(Integer userId,Integer deptId,Integer type) {
        try {
            deptService.allotUserByDept(userId,deptId,type);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    //获取部门负责人
    @RequestMapping("/getMasterUserByDept")
    public SysUser getMasterUserByDept(Integer deptId){
        return deptService.getMasterUserByDept(deptId);
    }


    //设置部门负责人
    @RequestMapping(value = "/setMasterUserByDept")
    public ProcessResult setMasterUserByDept(Integer userId,Integer deptId,Integer type) {
        try {
            deptService.setMasterUserByDept(userId,deptId,type);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }

    @RequestMapping("/getSysDeptUser")
    public SysDeptUser getSysDeptUser(Integer userId,Integer deptId){
        return deptService.getSysDeptUser(userId,deptId);
    }


    @RequestMapping("/showAllot")
    public ModelAndView showAllot() {
        return new ModelAndView("/modules/sys/sysDept/allotUser");
    }

}
